<?php
class CHboard extends CI_Model{
	private $outter;
	private $table_name;
	private $skin_list;
	private $skin_write;
	private $skin_view;
	private $config;

	
	function __construct($param) {	
		$this->config = $param['config'];
		$this->table_name = $param['table_name'];
		$this->load->model('board_model');
		$sval = file_get_contents(convert_path('/board/skin/'.$param['skin'].'/skin.html'));
		list($sval,$this->skin_list) = $this->cutSkinTag($sval,'board_list');
		list($sval,$this->skin_write) = $this->cutSkinTag($sval,'templ_write');
		list($sval,$this->skin_view) = $this->cutSkinTag($sval,'templ_view');
		$this->outter =  $sval;
		
	}
	
	function getOutter(){
		return $this->outter;
	}
	
	//@스킨에서 특정범위 태그 배열로 가져오기
	function cutSkinTag($contents,$tag,$replace=null){
		if(is_null($replace)){
			$replace="[##_{$tag}_##]";
		}
		$tagSize=strlen($tag)+4;
		$begin=strpos($contents,"<s_$tag>");
		if($begin===false)
			return array($contents,NULL);
		$end=strpos($contents,"</s_$tag>",$begin+4);
		if($end===false)
			return array($contents,NULL);
		$inner=substr($contents,$begin+$tagSize,$end-$begin-$tagSize);
		$outter=substr($contents,0,$begin).$replace.substr($contents,$end+$tagSize+1);
		return array($outter,$inner);
	}
	
	//@tag replace
	function dress($tag,$value,&$contents){
		if(preg_match("@\\[##_{$tag}_##\\]@iU",$contents)){
			$contents=str_replace("[##_{$tag}_##]",$value,$contents);
			return true;
		}else{
	
			return false;
		}
	}
	
	/**
	 * tag clean up
	 *
	 * @access	public
	 * @param   template
	 * @return	string
	 */
	function removeAllTags($contents){
		$contents=preg_replace('/\[#M_[^|]*\|[^|]*\|/Us','',str_replace('_M#]','',preg_replace('/\[##_.+_##\]/Us','',$contents)));
		$contents=preg_replace('@(<s_|</s_)[0-1a-zA-Z_]+>@','',$contents);
		return $contents;
	}
	
	/**
	 * 선택 태그범위 지움
	 *
	 * @access	public
	 * @param   tag,value,template
	 * @return	string
	 */
	function removeTagSelected($tag,$value,$content){
		//$tag = "button_1";
		$tagSize=strlen($tag)+4;
		$begin=strpos($content,"<s_$tag>");
		$end=strpos($content,"</s_$tag>",$begin+4);
		if($begin)
			return str_replace(substr($content,$begin+$tagSize,$end-$begin-$tagSize),$value,$content);
		else
			return $content;
	}
	
	/**
	 * 선택 태그범위 가져오기
	 *
	 * @access	public
	 * @param   tag,template
	 * @return	string
	 */
	function getTagSelected($tag,$content){
		//$tag = "button_1";
		$tagSize=strlen($tag)+4;
		$begin=strpos($content,"<s_$tag>");
		$end=strpos($content,"</s_$tag>",$begin+4);
		if($begin)
			return substr($content,$begin+$tagSize,$end-$begin-$tagSize);
		else
			return $content;
	}
	
	/**
	 * 게시판 목록
	 *
	 * @access	public
	 * @return	buffer
	 */
	function boardList($param){
		$view='';
		$param = array_merge($param,array('config'=>$this->config));
		$query = $this->board_model->board_list($param);
		if($query->result()){
			ob_start(); //출력버퍼시작
			foreach ($query->result() as $row)
			{
				$view = $this->skin_list;
				$replyCss = null;
				$replyIcon = null;
				if($row->depth > 0){ 
					$replyCss = "reply".$row->depth;
					$replyIcon = '<img src="/board/skin/'.$this->config['board_skin'].'/images/icon_re.gif"  />';
				}
				
				$query_file = $this->board_model->board_file_list(array('board_id'=>$param['id'],'board_no'=>$row->id));
				if($query_file->num_rows() > 0){
					$this->dress('brd_list_icon_file','<img src="/board/skin/'.$this->config['board_skin'].'/images/btn_file.gif"  />' ,$view);
				}
				$this->dress('brd_list_icon_reply',$replyIcon ,$view);
				$this->dress('brd_list_css_reply',$replyCss,$view);
				$this->dress('brd_list_no',$row->id,$view);
				$this->dress('brd_list_title',$row->title,$view);
				$this->dress('brd_list_writer',$row->writer,$view);
				$this->dress('brd_list_view_link','/front/board/view/id/'.$param['id'].'/no/'.$row->id,$view);
				$this->dress('brd_list_date',substr($row->register_date,0,10),$view);
				$this->dress('brd_list_visited',$row->view_count,$view);
				print $view;
				
			}
			$view = ob_get_contents(); //출력버퍼내용
			ob_end_clean(); //출력버퍼내용 초기화
		}
			
		return $view;
	}
	
	/**
	 * 게시판 쓰기
	 *
	 * @access	public
	 * @return	buffer
	 */
	function boardWrite($param){
		
		$dbjob = ($this->input->post('dbjob')) ? $this->input->post('dbjob') : 'i'; 
		
		ob_start(); //출력버퍼시작
		$view = $this->skin_write;
		$this->dress('skin_name',$this->config['board_skin'],$view);
		$this->dress('hidden_dbjob',$dbjob,$view);
		$this->dress('hidden_id',$param['id'],$view);
		$this->dress('hidden_no',$param['no'],$view);
		$this->dress('hidden_board_name',$param['board_name'],$view);
		
		$query = $this->board_model->board_view($param);
		if($query->num_rows() > 0){
			$row = $query->row_array();
			
			$this->dress('hidden_ref_id',$row['ref_id'],$view);
			$this->dress('hidden_depth',$row['depth'],$view);
			$this->dress('hidden_ansnum',$row['ansnum'],$view);
			
			$this->dress('title',$row['title'],$view);
			if($dbjob == 'u'){
				$this->dress('writer',$row['writer'],$view);
				$this->dress('content',$row['content'],$view);
				
				$query = $this->board_model->board_file_list(array('board_id'=>$param['id'],'board_no'=>$param['no']));
				$view_file = null;
				$_fnum = 0;
				foreach ($query->result() as $row){
					$this->dress('file_info'.($_fnum+1),'<span style="font-size:9pt;"><input type="checkbox" name="fdelete[]" value="'.$_fnum.'" /> 삭제</span> ',$view);
					$_fnum++;
				}
				$query->free_result(); //free result
			}
		}
		
		print $view;
		$view=ob_get_contents(); //출력버퍼내용
		ob_end_clean(); //출력버퍼내용 초기화
		return $view;
		
	}
	
	/**
	 * 게시판 상세
	 *
	 * @access	public
	 * @return	result
	 */
	function boardView($param){
		//게시물 내용
		$this->board_model->board_counter(array('no'=>$param['no'],'board_name'=>$this->table_name));
		
		$query_prev = $this->board_model->board_other_view(array('other'=>array('id > '=> $param['no']),'orderby'=>'id','board_name'=>$this->table_name));
		$query_next = $this->board_model->board_other_view(array('other'=>array('id < '=> $param['no']),'orderby'=>'id DESC','board_name'=>$this->table_name));
		
		$query = $this->board_model->board_view(array('no'=>$param['no'],'board_name'=>$this->table_name,'page'=>$param['page']));
		$row = $query->row_array();
		$query->free_result();  //free result
		
		ob_start(); //출력버퍼시작
		$view = $this->skin_view;
		$this->dress('theme',$param['theme'],$view);
		$this->dress('skin_name',$this->config['board_skin'],$view);
		$this->dress('hidden_id',$param['id'],$view);
		$this->dress('hidden_no',$param['no'],$view);
		$this->dress('hidden_board_name',$param['board_name'],$view);
		
		$this->dress('view_writer',$row['writer'],$view);
		$this->dress('view_title',$row['title'],$view);
		$this->dress('view_date',substr($row['register_date'],0,10),$view);
		$this->dress('view_content',$row['content'],$view);
		$this->dress('view_visited',$row['view_count'],$view);
		
		$this->dress('board_id',$param['id'],$view);
		$this->dress('board_no',$param['no'],$view);
		
		$this->dress('user_id','test',$view);
		$this->dress('comment_name','테스터',$view);

		//이전글
		if($query_prev->num_rows() > 0){
			$prev = $query_prev->row_array();
			$query_prev->free_result();  //free result
			$this->dress('view_prev_link','/front/board/view/id/'.$param['id'].'/no/'.$prev['id'],$view);
			$this->dress('view_prev_title',$prev['title'],$view);
			$this->dress('view_prev_writer',$prev['writer'],$view);
			$this->dress('view_prev_date',substr($prev['register_date'],0,10),$view);
		}
		//다음글
		if($query_next->num_rows() > 0){
			$next = $query_next->row_array();
			$query_next->free_result();  //free result
			$this->dress('view_next_link','/front/board/view/id/'.$param['id'].'/no/'.$next['id'],$view);
			$this->dress('view_next_title',$next['title'],$view);
			$this->dress('view_next_writer',$next['writer'],$view);
			$this->dress('view_next_date',substr($next['register_date'],0,10),$view);
		}
		
		//첨부파일
		$query = $this->board_model->board_file_list(array('board_id'=>$param['id'],'board_no'=>$param['no']));
		$view_file = null;
		foreach ($query->result() as $row){
			$download_link = '/board/download/id/'.$row->board_id.'/no/'.$row->board_no.'/order/'.$row->order_no;
			$view_file .= ($view_file) ? ' / <a href="'.$download_link.'">'.$row->file_orinal.'</a>' : '<a href="'.$download_link.'">'.$row->file_orinal.'</a>';  
		}
		$query->free_result(); //free result
		$this->dress('view_file',$view_file,$view);
		
		//댓글
		$query = board_model::board_comment_list(array('no'=>$param['no'],'board_name'=>'ch_board_comment_'.$param['id']));
		$comment = '';
		foreach ($query->result() as $row){
			$comment .= $this->getTagSelected('comment_list',$view);
			$this->dress('comment_no',$row->id,$comment);
			$this->dress('comment_name',$row->name,$comment);
			$this->dress('comment_date',substr($row->register_date,0,10),$comment);
			$this->dress('comment_content',nl2br(stripslashes($row->b_comment)),$comment);
			
		}

		$query->free_result(); //free result
		$view = $this->removeTagSelected('comment_list',$comment,$view);
						
		print $view;
		$view=ob_get_contents(); //출력버퍼내용
		ob_end_clean(); //출력버퍼내용 초기화
		return $view;
	
	}
}
?>